<?php

namespace Controller\Ajax;

class Base extends \Lib\Core\Ajax {

    public $mustLogin = true;
    public $accountId;
    public $accountInfo;

    public function response($data) {
        $data = array(
            'error' => 0,
            'data' => $data,
            'code' => 0,
        );
        echo json_encode($data);
        \main::sysExit();
    }

    public function responseError($msg, $code = 1) {
        $data = array(
            'error' => 1,
            'data' => $msg,
            'code' => 0,
        );
        echo json_encode($data);
        \main::sysExit();
    }

    protected function actionBefore() {
        $this->getCurrentAccount();
        if ($this->mustLogin) {
            $this->mustLogin();
        }
    }

    private function getCurrentAccount() {
        $id = \Util\Account::getAccountIdToCookie();
        if (!$id) {
            return;
        }
        $info = \Business\Account::instance()->getInfoById($id);
        if (!$info) {
            return;
        }
        $this->accountInfo = $info;
        $this->accountId = $id;
    }

    protected function mustLogin() {
        if (!$this->accountId) {
            $this->responseError('未登录');
        }
        return true;
    }

}